<template>
  <div class="container">
    <div class="app-container">
      <!-- :data可以渲染里面的数组 props {修改绑定的名字}   default-expand-all 展开-->
      <el-tree default-expand-all :data="depts" :props="{ label: 'name' }">
        <!-- 会传递2个参数   date 表示 当前节点的数据-->
        <template #default="{ data }">
          <el-row style="width: 100%">
            <el-col :span="20">{{ data.name }}</el-col>
            <el-col :span="4">
              <el-row>
                <el-col :span="12">{{ data.managerName ||'暂无' }}</el-col>
                <el-col :span="12">
                  <el-dropdown @command="handerCommand($event,data.id)">
                    <!-- 显示区域内容 -->
                    <span class="el-dropdown-link">
                      操作<i class="el-icon-arrow-down el-icon--right" />
                    </span>
                    <!-- 下拉菜单选项 -->
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                      <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                      <el-dropdown-item command="del">删除</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown></el-col>
              </el-row>

              <!-- $event 实参 表示类型 -->
            </el-col>
          </el-row>
        </template>
      </el-tree>
      <!-- 新增部门 -->
      <add-depat ref="ediRef" :show-dialog.sync="showDialog" :souce-dpts="souceDpts" :pid="pid" @reload="getDepts" />
    </div>
  </div>
</template>
<script>
import { reqGetDepts, reqdeleEdit } from '@/api/department'
import { translateTreeData } from '@/utils/index'
import addDepat from './components/add-depat.vue'
export default {
  name: 'Department',
  components: { addDepat },
  data() {
    return {
      depts: [],
      showDialog: false,
      souceDpts: [],
      pid: 0
    }
  },
  created() {
    this.getDepts()
  },
  methods: {
    async getDepts() {
      const res = await reqGetDepts()

      this.souceDpts = res.data
      this.depts = translateTreeData(res.data, 0)
    // 一级部门有一个pid为1 二级部门pid=一级部门的id?
    },
    handerCommand(command, id) {
      if (command === 'add') {
        this.showDialog = true
        this.pid = id
        this.$refs.ediRef.title = '新增'
        // console.log(1)
      }
      // 编辑
      if (command === 'edit') {
        this.showDialog = true
        this.$refs.ediRef.title = '编辑'
        // 点击编辑展示对话框 通过$ref拿到子组件的实例方法
        // this.$refs.ediRef.showEdiDept()
        // console.log(2)
        // pid=id 传id过去
        this.pid = id
        // console.log(id)
        // this.$refs.ediRef.showEdiDept(id)

        this.$nextTick(() => {
          // dom 更新是异步的不能实施显示id
          this.$refs.ediRef.showEdiDept()
        })
      }
      if (command === 'del') {
        // console.log(3)
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          await reqdeleEdit(id)
          this.getDepts()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      }
    }

  }
}
</script>
<style lang="scss" scoped>
.app-container {
  padding: 30px 140px;
  font-size: 14px;
}
.tree-manager {
  width: 50px;
  display: inline-block;
  margin: 10px;
}
</style>
